package com.app.micaihu.view.army.activity

import android.os.Bundle
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.adapter.ArmyOfficialPositionListAdapter
import com.app.micaihu.view.army.contract.ArmyMemberContract
import com.app.micaihu.view.army.presenter.ArmyMemberPresenter
import com.app.micaihu.view.bean.ArmyGrade
import com.app.micaihu.view.bean.ArmyMember
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.view_common_title_rv.*

/**
 * 军团官职列表
 */
class ArmyOfficialPositionListActivity : BaseMvpActivity<ArmyMemberContract.View, ArmyMemberPresenter>(), ArmyMemberContract.View {

    override fun getLayoutId() = R.layout.view_common_title_rv

    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
    }

    override fun getPresenter() = ArmyMemberPresenter(gid)

    private var gid = ""

    private val armyOfficialPositionListAdapter: ArmyOfficialPositionListAdapter by lazy {
        val armyOfficialPositionListAdapter = ArmyOfficialPositionListAdapter()
        rvList.layoutManager = WrapContentLinearLayoutManager(thisContext)
        rvList.adapter = armyOfficialPositionListAdapter
        armyOfficialPositionListAdapter
    }

    override fun setData() {
        mPresenter?.armyGradeSet()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            mPresenter?.armyGradeSet()
        }
        armyOfficialPositionListAdapter.setOnItemClickListener { adapter, view, position ->
            val item = armyOfficialPositionListAdapter.getItem(position)
            if (1 == item.permission) {
                val bundle = Bundle()
                bundle.putString(Config.Extra.id, gid)
                bundle.putString(Config.Extra.level, item.level)
                enterPager(ArmyOfficialPositionCurrentListActivity::class.java, bundle)
            } else {
                ToastUtils.showShort("没有权限")
            }

        }
    }

    override fun handleArmyGradeSet(result: BaseBean<List<ArmyGrade>?>?) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        if (result == null) {
            armyOfficialPositionListAdapter.setEmptyView(errorView)
        } else {
            if (result.isSuccess()) {
                armyOfficialPositionListAdapter.setEmptyView(getEmptyView())
                result.data?.let {
                    armyOfficialPositionListAdapter.setList(it)
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }
    }


}